package com.example.firstapp.fragment

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.fragment.app.replace
import com.example.firstapp.R

class fragment2 : AppCompatActivity() {
//    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fragment2)
        println(1)
        replaceFragment(rightFragment1())
        println(2)
        val fragmentbutton:Button = findViewById(R.id.fragmentbutton)
        fragmentbutton.setOnClickListener {
            replaceFragment(rightFragment2())
            println(3)
        }

//        val leftfragmentmethod1:Button = findViewById(R.id.leftfragmentmethod1)
//        val fragment: Fragment? = supportFragmentManager.findFragmentById(R.id.leftFrag)
//        leftfragmentmethod1.setOnClickListener {
//            fragment.
//        }
    }

    private fun replaceFragment(fragment: Fragment){
        // 获取FragmentManager
        val fragmentManager = supportFragmentManager
        // 开启一个事务
        val transaction = fragmentManager.beginTransaction()
        // 向容器内添加或替换Fragment
        transaction.replace(R.id.rightLayout, fragment)
        //将事务添加到返回栈中（选用）
        transaction.addToBackStack(null)
        // 提交事务
        transaction.commit()
    }
}